package com.kassadin.server.tcp;

import com.kassadin.server.Contants;
import com.kassadin.server.ServerBootStrapFactory;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.epoll.EpollEventLoopGroup;
import io.netty.channel.epoll.EpollServerSocketChannel;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * ClassName: TcpServer
 * Description: tcp的Server端
 *
 * @author kang.wang03
 *         Date 16/3/15
 */
public class TcpServer {
    private final ServerBootstrap bootstrap;
    private final EventLoopGroup bossGroup;
    private final EventLoopGroup workerGroup;

    public TcpServer(int workerMaxThread, TcpInitializer tcpInitializer) {
//        判断是否采用epoll模型
        bootstrap = ServerBootStrapFactory.createServerBootstrap(1, workerMaxThread);
        this.bossGroup = bootstrap.group();
        this.workerGroup = bootstrap.childGroup();
        bootstrap.childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) throws Exception {
                tcpInitializer.initChannel(ch);
            }
        });

        try {
            ChannelFuture channelFuture = bootstrap.bind(8081).sync();
            channelFuture.channel().close().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    void bootChannelHandler() {

    }
}
